package com.rongke.web;

import com.rongke.commons.JsonResp;
import com.rongke.model.Machine;
import com.rongke.model.Muser;
import com.rongke.service.MachineService;
import com.rongke.service.MuserService;
import com.rongke.utils.PubLib;
import com.rongke.utils.QRCodeUtil2;
import com.rongke.utils.RequestUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * @MachineController
 * @机器Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/machine")
@Transactional
@CrossOrigin
public class MachineController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private MachineService machineService;
    @Autowired
    private MuserService muserService;
    /**
     * @添加机器
     * @param machine
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addMachine(@RequestBody Machine machine){
        log.debug("添加机器");
        String machineCode=machine.getMachineCode();
        Long userId=machine.getMuserId();
        Muser muser=muserService.selectById(userId);
        muser.setMachineCode(machineCode);
        muserService.updateById(muser);
        machineService.insert(machine);

        return JsonResp.ok(machine);
    }

    /**
     * @修改机器
     * @param machine
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateMachine(@RequestBody Machine machine){
        log.debug("修改机器");
        Machine machineOld=machineService.selectById(machine.getId());
        machineOld.setAddress(machine.getAddress());
        machineOld.setIntroducerId(machine.getIntroducerId());
        machineOld.setMuserId(machine.getMuserId());
        machineOld.setMachineCode(machine.getMachineCode());
        machineOld.setName(machine.getName());
        machineService.updateById(machineOld);
        return JsonResp.ok("","成功");
    }
    /**
     * @根据id查找机器
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectMachine(Long id){
        log.debug("查找机器");
        Machine machine = machineService.selectById(id);
        return JsonResp.ok(machine);
    }

    /**
     * @添加机器
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/delOne", method = RequestMethod.GET)
    public JsonResp delOne(Long id){
        log.debug("添加机器");
        machineService.deleteById(id);
        return JsonResp.ok("删除成功");
    }

    /**
     * @根据条件查找机器
     * @param machine
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectMachineByCondition(@RequestBody Machine machine){
        log.debug("根据条件查找机器");
        if(machine==null)
            return JsonResp.toFail("参数不能为空");
        List<Machine> machineList=machineService.selectByCondition(machine);
        return JsonResp.ok(machineList);
    }

    /**
     * @获取机器详情
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/findDetail", method = RequestMethod.GET)
    public JsonResp findDetail(Long id){
        log.debug("获取机器详情");
        Machine machine=machineService.selectById(id);
        return JsonResp.ok(machine);
    }



    /**
     * @生成机器二维码
     * @param machineCode 会员id
     *        introducerId 介绍人id
     *         id  机器id
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/generateMachineQrCode", method = RequestMethod.GET)
    public JsonResp generateMachineQrCode(String machineCode,Long id, HttpServletRequest request){
        log.debug("生成机器二维码");
        Machine machine=machineService.selectById(id);
        String link="http://www.meedong.com/wechat/goods/variety/"+machineCode;
        String url= RequestUtils.getHostPath(request);
        String path= PubLib.getFileDir(request)+ File.separator + UUID.randomUUID()+".png";
        File saveFile = new File(path);
        String logoPath=PubLib.getFileDir(request)+ File.separator+"android-template.png";
        try {
//            QRCodeUtil.encode(link,"",path,false,600);
            QRCodeUtil2.encode(link,600,600,logoPath,path);
        } catch (Exception e) {
            return JsonResp.fa("机器二维码生成失败");
        }
        String qrCode=url+"/"+saveFile.getParentFile().getName()+"/"+saveFile.getName();
        machine.setMachineQrCode(qrCode);
        machineService.updateById(machine);
        return JsonResp.ok("","成功");
    }



}
